import { h } from 'vue';

import { message, Modal } from 'ant-design-vue';

import { auto1688stor } from '#/store';

const buyDlyAppLink = import.meta.env.VITE_APP_BUY_DOUDIAN_LINK;
const buyOrderAppLink = import.meta.env.VITE_APP_XIADAN_LINK;
export function checkBingShops() {
  const auto1688 = auto1688stor();

  const { bind1688, binddy } = auto1688.auto1688;

  if (!bind1688) {
    message.error('请先绑定1688平台');
    return false;
  }
  if (!binddy) {
    // message.error('请先绑定抖店');
    Modal.info({
      title: '提示',
      content: h('div', {}, [h('p', '需要订购授权后使用')]),
      onOk() {
        // open(import.meta?.env?.VITE_APP_BUY_DOUDIAN_LINK);
        open(buyDlyAppLink);
        // console.log('OK'); 前往抖店
      },
      okText: '前往授权使用',
      cancelText: '取消',
      maskClosable: true,
      keyboard: true,
    });
    return false;
  }
  return true;
}

// 检查当前所选的店铺是否已绑定订单应用
export function checkBindOrderApp(shopId: any) {
  const auto1688 = auto1688stor();

  const { bindShopList } = auto1688.auto1688;
  if (!shopId) {
    message.error('请先选择店铺');
    return false;
  }
  // console.log('bindShopList', nowSelectDyShop);
  const getIsOrderApp = bindShopList.find((item) => {
    return item.shopId === shopId && item.pType === 3;
  });
  const getShopName = bindShopList.find((item) => {
    return item.shopId === shopId;
  });
  if (!getIsOrderApp) {
    Modal.confirm({
      title: '提示',
      content: h('div', {}, [
        h(
          'p',
          `店铺${getShopName?.shopName},当前功能需要授权订单应用，请授权后使用`,
        ),
      ]),
      okText: '前往授权使用',
      onOk() {
        // open(import.meta?.env?.VITE_APP_BUY_DOUDIAN_LINK);
        open(buyOrderAppLink, '_blank');
      },
    });
    // message.error('请先绑定订单应用');
    return false;
  }
  return true;
  // console.log('getIsOrderApp', getIsOrderApp);
}
